import { createStore, applyMiddleware } from 'redux';

import thunk from 'redux-thunk';
import { composeWithDevTools } from 'redux-devtools-extension';

import rootReducer from './reducers/index';
import { getToken } from '@/utils/token';

// 创建中间件的集合
// const middleware = composeWithDevTools(applyMiddleware(thunk));

// 根据环境判断是否需要使用浏览器插件查看redux状态
const middleware = () => {
  if (process.env.NODE_ENV === 'production') {
    return applyMiddleware(thunk);
  } else {
    return composeWithDevTools(applyMiddleware(thunk));
  }
};

// 给store 状态赋初始值

const initStore = {
  login: getToken(),
};

// 创建store
const store = createStore(rootReducer, initStore, middleware());

export default store;
